# Maintainer: Sébastien Luttringer <seblu@aur.archlinux.org>
# Contributor: Juergen Hoetzel <juergen@archlinux.org>
# Contributor: Damir Perisa <damir.perisa@bluewin.ch>

pkgname=lua51
pkgver=5.1.5
pkgrel=9
pkgdesc='Powerful lightweight programming language designed for extending applications'
arch=('x86_64')
url='https://www.lua.org/'
depends=('readline')
license=('MIT')
options=('!makeflags' '!emptydirs')
source=(https://www.lua.org/ftp/lua-$pkgver.tar.gz
        lua-arch.patch
        lua-5.1-cflags.diff)
sha512sums=('0142fefcbd13afcd9b201403592aa60620011cc8e8559d4d2db2f92739d18186860989f48caa45830ff4f99bfc7483287fd3ff3a16d4dec928e2767ce4d542a9'
            '7678986a9c5310a81c260cde0124d14f5c2dffebf0bc61a257ec1e39f63b5ec0bc2a6adf5c39cdbc45ba4594487f46bfafd20cb334ae965f3379c244b338fa00'
            'cd135a2e6583ec0b059133b4383ccecb4d8c7ac668fc22be0ef37dc7ac55e82e9afe52bc7c0aa39f40fdbf30a1657d7426fce0418d0a26d22f1058c2647d1a67')

prepare() {
  cd lua-$pkgver
  patch -p1 -i ../lua-arch.patch
  patch -p1 -i ../lua-5.1-cflags.diff

  sed -e 's:llua:llua5.1:' -e 's:/include:/include/lua5.1:' -i etc/lua.pc
  sed -r -e '/^LUA_(SO|A|T)=/ s/lua/lua5.1/' -e '/^LUAC_T=/ s/luac/luac5.1/' -i src/Makefile
}

build() {
  cd lua-$pkgver

  [[ $CARCH == x86_64 ]] && export CFLAGS="$CFLAGS -fPIC"
  make MYCFLAGS="$CFLAGS" MYLDFLAGS="$LDFLAGS" linux
}

package() {
  cd lua-$pkgver

  make \
    TO_BIN='lua5.1 luac5.1' \
    TO_LIB="liblua5.1.a liblua5.1.so liblua5.1.so.5.1 liblua5.1.so.$pkgver" \
    INSTALL_DATA='cp -d' \
    INSTALL_TOP="$pkgdir"/usr \
    INSTALL_INC="$pkgdir"/usr/include/lua5.1 \
    INSTALL_MAN="$pkgdir"/usr/share/man/man1 \
    install

  install -Dm644 etc/lua.pc "$pkgdir"/usr/lib/pkgconfig/lua51.pc
  ln -sf lua51.pc "$pkgdir"/usr/lib/pkgconfig/lua5.1.pc
  ln -sf lua51.pc "$pkgdir"/usr/lib/pkgconfig/lua-5.1.pc

  install -d "$pkgdir"/usr/share/doc/$pkgname
  install -m644 doc/*.{gif,png,css,html} "$pkgdir"/usr/share/doc/$pkgname
  install -Dm644 COPYRIGHT "$pkgdir"/usr/share/licenses/$pkgname/COPYRIGHT

  ln -s liblua5.1.so "$pkgdir"/usr/lib/liblua.so.5.1
  ln -s liblua5.1.so "$pkgdir"/usr/lib/liblua.so.$pkgver

  cd "$pkgdir"/usr/share/man/man1
  mv lua.1 lua5.1.1
  mv luac.1 luac5.1.1
}
